<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		GNU Lesser GPL
*/

// No direct access.
defined('_JEXEC') or die();

class JFormFieldCategory extends JFormField
{
	protected $type 		= 'category';

	protected function getInput() {
		$db = & JFactory::getDBO();
		$first[]  = array('value'=>0,'text'=>JText::_("COM_DANIPRO_SELECT_CATEGORY"));
		$id = JRequest::getVar('id');
		$db->setQuery("select id,title,id as value,title as text,parent from #__dani_categories where published=1 order by title");
		$rows = $db->loadObjectList();
		
		$levellimit = 100;
		$children = array();
		// first pass - collect children
		foreach ($rows as $v )
		{
			$pt = $v->parent;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push( $list, $v );
			$children[$pt] = $list;
		}

		// second pass - get an indent list of the items
		$list = DaniProHelper::treerecurse(0, '', array(), $children, max( 0, $levellimit-1 ));
		
		if($list)
			$options  = array_merge($first,$list);
		else
			$options = $first;
		return JHTML::_("select.genericlist",$options,'jform['.$this->element['name'].']',$this->element['class'],'value','text',$this->value);
	}
}
?>